package org.dromara.event.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.stereotype.Service;
import org.dromara.event.domain.bo.EventAuditBo;
import org.dromara.event.domain.vo.EventAuditVo;
import org.dromara.event.domain.EventAudit;
import org.dromara.event.mapper.EventAuditMapper;
import org.dromara.event.service.IEventAuditService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 活动审核Service业务层处理
 *
 * @author Lion Li
 * @date 2025-04-24
 */
@RequiredArgsConstructor
@Service
public class EventAuditServiceImpl implements IEventAuditService {

    private final EventAuditMapper baseMapper;

    /**
     * 查询活动审核
     *
     * @param id 主键
     * @return 活动审核
     */
    @Override
    public EventAuditVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询活动审核列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 活动审核分页列表
     */
    @Override
    public TableDataInfo<EventAuditVo> queryPageList(EventAuditBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EventAudit> lqw = buildQueryWrapper(bo);
        Page<EventAuditVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的活动审核列表
     *
     * @param bo 查询条件
     * @return 活动审核列表
     */
    @Override
    public List<EventAuditVo> queryList(EventAuditBo bo) {
        LambdaQueryWrapper<EventAudit> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EventAudit> buildQueryWrapper(EventAuditBo bo) {
        LambdaQueryWrapper<EventAudit> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(EventAudit::getId);
        lqw.eq(bo.getEventId() != null, EventAudit::getEventId, bo.getEventId());
        lqw.eq(bo.getUserId() != null, EventAudit::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getApplyStatus()), EventAudit::getApplyStatus, bo.getApplyStatus());
        lqw.eq(bo.getAuditUserId() != null, EventAudit::getAuditUserId, bo.getAuditUserId());
//        lqw.between(params.get("beginAuditTime") != null && params.get("endAuditTime") != null,
//            EventAudit::getAuditTime ,params.get("beginAuditTime"), params.get("endAuditTime"));
        lqw.eq(StringUtils.isNotBlank(bo.getAuditRemark()), EventAudit::getAuditRemark, bo.getAuditRemark());
        return lqw;
    }

    /**
     * 新增活动审核
     *
     * @param bo 活动审核
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(EventAuditBo bo) {
        EventAudit add = MapstructUtils.convert(bo, EventAudit.class);
        validEntityBeforeSave(add);
        add.setCreateTime(new Date());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改活动审核
     *
     * @param bo 活动审核
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(EventAuditBo bo) {
        EventAudit update = MapstructUtils.convert(bo, EventAudit.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }


    @Override
    public Boolean updateTpye(EventAuditBo bo) {
        EventAudit update = MapstructUtils.convert(bo, EventAudit.class);
        update.setUpdateTime(new Date());
        update.setAuditUserId(LoginHelper.getUserId());
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EventAudit entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除活动审核信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public Boolean audit(Long id,String auditStatus) {

        return baseMapper.update(
            null,
            Wrappers.lambdaUpdate(EventAudit.class)
                .set(EventAudit::getApplyStatus, auditStatus)
                .set(EventAudit::getAuditUserId, LoginHelper.getUserId())
                .eq(EventAudit::getId, id)
        )> 0;
    }
}
